//= require ember/ember

api = 'https://api.github.com'

App = Ember.Application.create
    LOG_TRANSITIONS: true

App.Router.map ->
    @resource 'users'
    @resource 'user', path: 'users/:user_id', ->
        @resource 'repos'
        @resource 'gists'
        @resource 'followers'
        @resource 'following'

App.UsersRoute = Ember.Route.extend
    model: ->
        $.getJSON(api + '/users?callback=?').then (data)->
            data.data

App.UserRoute = Ember.Route.extend
    model: (params)->
        $.getJSON(api + "/users/#{params.user_id}?callback=?").then (data)->
            data.data

App.ReposRoute = Ember.Route.extend
    model: (params)->
        user = @modelFor('user')
        $.getJSON(api + "/users/#{user.login}/repos?callback=?").then (data)->
            data.data

App.GistsRoute = Ember.Route.extend
    model: (params)->
        user = @modelFor('user')
        $.getJSON(api + "/users/#{user.login}/gists?callback=?").then (data)->
            data.data.map (gist)->
                for name,file of gist.files
                    gist[k] = v for k,v of file
                gist

App.FollowersRoute = Ember.Route.extend
    model: (params)->
        user = @modelFor('user')
        $.getJSON(api + "/users/#{user.login}/followers?callback=?").then (data)->
            data.data

App.FollowingRoute = Ember.Route.extend
    model: (params)->
        user = @modelFor('user')
        $.getJSON(api + "/users/#{user.login}/following?callback=?").then (data)->
            data.data


